import axios from 'axios';
import { message } from 'antd';

// 修改传入的 marketData 数组
export const fetchMarketData = async (amt = 1, marketData) => {
  const token = localStorage.getItem("jwt_token");

  try {
    for (let page = 1; page <= amt; page++) {
      const res = await axios.get(`/api/ui/stocklist?page=${page}`, {
        headers: { Authorization: `Bearer ${token}` },
      });

      console.log(`第 ${page} 页数据:`, res.data);

      if (res.data.code === 0) {
        const stocksPage = res.data.data.map(stock => ({
          股票代码: stock.股票代码,
          股票名称: stock.股票名称,
          最新价: stock.最新价,
          涨跌幅: stock.涨跌幅,
          涨跌额: stock.涨跌额,
          成交量: stock.成交量,
          振幅: stock.振幅,
          最高: stock.最高,
          最低: stock.最低,
          今开: stock.今开,
          昨收: stock.昨收,
          市盈率: stock.市盈率,
          换手率: stock.换手率
        }));
        marketData.push(...stocksPage); // 修改传入的 marketData
      }
    }
    return marketData;
  } catch (error) {
    console.error("获取市场数据失败:", error);
    message.error("获取市场数据失败，请稍后重试");
    return [];
  }
};
